/******************************************************************
 *
 *    Java Lib For JavaWeb, Powered By Zhejiang hangzhou.
 *
 *    Copyright (c) 2001-2020
 *    http://tankpao.tk/
 *
 *    Package:     tk.tankpao.blast.util
 *
 *    Filename:    PointcutPathUtils.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2020
 *
 *    Company:     tankpao.tk
 *
 *    @author:     王志刚
 *
 *    @version:    1.0.0
 *
 *    Create at:   2017年2月14日 下午4:45:25
 *
 *    Revision:
 *
 *    2017年2月14日 下午4:45:25
 *        - first revision
 *
 *****************************************************************/
package tk.tankpao.blast.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PointcutPathUtils
 * @Description aop path路径处理
 * @author 王志刚
 * @Date 2017年2月14日 下午4:45:25
 * @version 1.0.0
 */
public class PointcutPathUtils {
	
	public static String MAIN_SEG_STR = ",";
	public static String SUB_SEG_STR = "#";
	public static String PACKAGE_WILDCARD = "..";
	public static String CLASS_AND_METHOD_WILDCARD = "*";
	
	public static boolean accordPath(String pointcut, Class clazz){
		String targetName = clazz.getName();
		String packagePath = targetName.substring(0, targetName.lastIndexOf("."));
		String className = targetName.replace(packagePath+".", "");
		
		String[] pathItems = pointcut.trim().split(SUB_SEG_STR);
		boolean packageAccord = false;
		boolean classAccord = false;
		//先判断是否匹配包名
		if(pathItems[0].indexOf(PACKAGE_WILDCARD) != -1){
			String prePackage = pathItems[0].substring(0, pathItems[0].indexOf(PACKAGE_WILDCARD));
			String sufPackage = pathItems[0].substring(pathItems[0].indexOf(PACKAGE_WILDCARD) + PACKAGE_WILDCARD.length());
			//PACKAGE_WILDCARD 通配符前后匹配
			if(packagePath.startsWith(prePackage) && packagePath.replace(prePackage, "").endsWith(sufPackage)){
				packageAccord = true;
			}
		}else if(pathItems[0].equals(packagePath)){  //包名没有通配符    判断匹配全路径
			packageAccord = true;
		}
		
		if(packageAccord){
			if(pathItems[1].indexOf(CLASS_AND_METHOD_WILDCARD) != -1){
				String preClass = pathItems[1].substring(0, pathItems[1].indexOf(CLASS_AND_METHOD_WILDCARD));
				String sufClass = pathItems[1].substring(pathItems[1].indexOf(CLASS_AND_METHOD_WILDCARD) + CLASS_AND_METHOD_WILDCARD.length());
				//CLASS_AND_METHOD_WILDCARD 通配符前后匹配
				if(className.startsWith(preClass) && className.replace(preClass, "").endsWith(sufClass)){
					classAccord = true;
				}
			}else if(pathItems[1].equals(className)){
				classAccord = true;
			}
		}
		
		return classAccord;
	}
	
	// 类似tk.tankpao..#*#*ss
	public static Map<String,Boolean> accordMethods(String pathstr, Object targetObj){
		Map<String, Boolean> exeFlagMap = new HashMap<String, Boolean>();
		Method[] tarMethods = targetObj.getClass().getDeclaredMethods();
		
		String targetName = targetObj.getClass().getName();
		String packagePath = targetName.substring(0, targetName.lastIndexOf("."));
		String className = targetName.replace(packagePath+".", "");
		String[] paths = pathstr.split(MAIN_SEG_STR);
		for(String path : paths){
			String[] pathItems = path.trim().split(SUB_SEG_STR);
			boolean packageAccord = false;
			boolean classAccord = false;
			//先判断是否匹配包名
			if(pathItems[0].indexOf(PACKAGE_WILDCARD) != -1){
				String prePackage = pathItems[0].substring(0, pathItems[0].indexOf(PACKAGE_WILDCARD));
				String sufPackage = pathItems[0].substring(pathItems[0].indexOf(PACKAGE_WILDCARD) + PACKAGE_WILDCARD.length());
				//PACKAGE_WILDCARD 通配符前后匹配
				if(packagePath.startsWith(prePackage) && packagePath.replace(prePackage, "").endsWith(sufPackage)){
					packageAccord = true;
				}
			}else if(pathItems[0].equals(packagePath)){  //包名没有通配符    判断匹配全路径
				packageAccord = true;
			}
			
			if(packageAccord){
				if(pathItems[1].indexOf(CLASS_AND_METHOD_WILDCARD) != -1){
					String preClass = pathItems[1].substring(0, pathItems[1].indexOf(CLASS_AND_METHOD_WILDCARD));
					String sufClass = pathItems[1].substring(pathItems[1].indexOf(CLASS_AND_METHOD_WILDCARD) + CLASS_AND_METHOD_WILDCARD.length());
					//CLASS_AND_METHOD_WILDCARD 通配符前后匹配
					if(className.startsWith(preClass) && className.replace(preClass, "").endsWith(sufClass)){
						classAccord = true;
					}
				}else if(pathItems[1].equals(className)){
					classAccord = true;
				}
			}
			if(classAccord){
				if(pathItems[2].indexOf(CLASS_AND_METHOD_WILDCARD) != -1){
					String preMethod = pathItems[2].substring(0, pathItems[2].indexOf(CLASS_AND_METHOD_WILDCARD));
					String sufMethod = pathItems[2].substring(pathItems[2].indexOf(CLASS_AND_METHOD_WILDCARD) + CLASS_AND_METHOD_WILDCARD.length());
					//CLASS_AND_METHOD_WILDCARD 通配符前后匹配
					for(Method tarMethod : tarMethods){
						if(tarMethod.getName().startsWith(preMethod) && tarMethod.getName().replace(preMethod, "").endsWith(sufMethod)){
							exeFlagMap.put(targetName+"."+tarMethod.getName(), true);
						}
					}
				}else{
					for(Method tarMethod : tarMethods){
						if(pathItems[2].equals(tarMethod.getName())){
							exeFlagMap.put(targetName+"."+tarMethod.getName(), true);
						}
					}
				}
			}
		}
		
		return exeFlagMap;
	}

	public static void main(String[] args) {
		String a = "asda..sd";
		System.out.println(a.substring(0, a.indexOf("..")));
		System.out.println(a.substring(a.indexOf("..") + 2));
		System.out.println(a.startsWith(""));
	}
}
